#include "h_iic_s_user.h"



#include "h_stm32_hal_defines.h"
#include "h_stm32_hal_timer.h"

#include "h_iic_s.h"

h_iic_s_t h_iic_s_data_t;

#define IIC_SCL_PIN					GPIO_PIN_5
#define IIC_SCL_PORT				GPIOA
#define IIC_SDA_PIN					GPIO_PIN_4
#define IIC_SDA_PORT				GPIOA

static void h_iic_sda_out_init(void)
{
	GPIO_InitTypeDef ds18b20_GPIO_InitStruct = {0};

	ds18b20_GPIO_InitStruct.Pin = IIC_SDA_PIN;
	ds18b20_GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;
	ds18b20_GPIO_InitStruct.Pull = GPIO_NOPULL;
	ds18b20_GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
	HAL_GPIO_Init(IIC_SDA_PORT, &ds18b20_GPIO_InitStruct);
}

static void h_iic_sda_out_high(void)
{
	// IIC_SDA_PORT->BSRR = IIC_SDA_PIN;
	HAL_GPIO_WritePin(IIC_SDA_PORT, IIC_SDA_PIN, GPIO_PIN_SET);
}

static void h_iic_sda_out_low(void)
{
	// IIC_SDA_PORT->BRR = IIC_SDA_PIN;
	HAL_GPIO_WritePin(IIC_SDA_PORT, IIC_SDA_PIN, GPIO_PIN_RESET);
}

static void h_iic_sda_in_init(void)
{
	GPIO_InitTypeDef ds18b20_GPIO_InitStruct = {0};

	ds18b20_GPIO_InitStruct.Pin = IIC_SDA_PIN;
	ds18b20_GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
	ds18b20_GPIO_InitStruct.Pull = GPIO_NOPULL;
	ds18b20_GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
	HAL_GPIO_Init(IIC_SDA_PORT, &ds18b20_GPIO_InitStruct);
}

static signed char h_iic_sda_read(void)
{
	return HAL_GPIO_ReadPin(IIC_SDA_PORT, IIC_SDA_PIN);
}

static void h_iic_scl_out_init(void)
{
	GPIO_InitTypeDef ds18b20_GPIO_InitStruct = {0};

	ds18b20_GPIO_InitStruct.Pin = IIC_SCL_PIN;
	ds18b20_GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;
	ds18b20_GPIO_InitStruct.Pull = GPIO_NOPULL;
	ds18b20_GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
	HAL_GPIO_Init(IIC_SCL_PORT, &ds18b20_GPIO_InitStruct);
}

static void h_iic_scl_out_high(void)
{
	// IIC_SCL_PORT->BSRR = IIC_SCL_PIN;
	HAL_GPIO_WritePin(IIC_SCL_PORT, IIC_SCL_PIN, GPIO_PIN_SET);
}

static void h_iic_scl_out_low(void)
{
	// IIC_SCL_PORT->BRR = IIC_SCL_PIN;
	HAL_GPIO_WritePin(IIC_SCL_PORT, IIC_SCL_PIN, GPIO_PIN_RESET);
}

#include <stdio.h>

void h_iic_s_user_init(void)
{
	// PC2 SCL
	// PB10 SDA
	h_iic_s_data_t.sda_out_init = &h_iic_sda_out_init;
	h_iic_s_data_t.sda_high = &h_iic_sda_out_high;
	h_iic_s_data_t.sda_low = &h_iic_sda_out_low;
	h_iic_s_data_t.sda_in_init = &h_iic_sda_in_init;
	h_iic_s_data_t.sda_read = &h_iic_sda_read;
	h_iic_s_data_t.scl_out_init = &h_iic_scl_out_init;
	h_iic_s_data_t.scl_high = &h_iic_scl_out_high;
	h_iic_s_data_t.scl_low = &h_iic_scl_out_low;
	h_iic_s_data_t.delay_us = &delay_us;

	h_iic_s_init(&h_iic_s_data_t);

	// h_iic_s_state rslt;

	// unsigned char address = 0x78, reg = 0x80;
	// unsigned char wdata[1] = {0x99};

	// h_iic_s_init(&h_iic_s_data_t);

	// rslt = h_iic_s_write_bytes(&h_iic_s_data_t, address, reg, wdata, 1);

	// printf("iic: %d\r\n", rslt);
}

signed char h_iic_user_write_bytes(unsigned char address, unsigned char reg, unsigned char *data, unsigned short len)
{
	return (signed char)h_iic_s_write_bytes(&h_iic_s_data_t, address, reg, data, len);
}

signed char h_iic_user_read_bytes(unsigned char address, unsigned char reg, unsigned char *data, unsigned short len)
{
	return (signed char)h_iic_s_read_bytes(&h_iic_s_data_t, address, reg, data, len);
}

void h_iic_user_test(void)
{
    unsigned char data[5];
    unsigned char addr = 0;
    for(; addr < 255; addr++) {
        if(!h_iic_user_read_bytes(addr, 0x00, data, 1)) {
            printf("Get 0x%02x\r\n", addr);
        } else {
            printf(".");
        }
    }
}
